package com.qfedu.service.impl;

import com.qfedu.dao.AccountDao;
import com.qfedu.pojo.Account;
import com.qfedu.service.AccountService;
import com.qfedu.util.TransactionManager;

import java.util.List;


public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;
//    private TransactionManager transactionManager;
//
//    public void setTransactionManager(TransactionManager transactionManager) {
//        this.transactionManager = transactionManager;
//    }

    //使用set方法注入
    public void setAccountDao(AccountDao accountDao1) {
        this.accountDao = accountDao1;
    }

    public void transfer(String sourceName, String targetName, Double money) {
        //根据名称查询出转账用户
        Account sourceAccount = accountDao.findAccountByName(sourceName);
        //根据名称查询转入账用户
        Account targetAccount = accountDao.findAccountByName(targetName);
        //转出账户扣钱
        sourceAccount.setMoney(sourceAccount.getMoney() - money);
        //转入账户加钱
        targetAccount.setMoney(targetAccount.getMoney() + money);
        //更新转出账户
        accountDao.updateAccount(sourceAccount);
//        int i = 1/0;
        //更新转入账户
        accountDao.updateAccount(targetAccount);
    }

}
